<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="osgi,karaf,bndtools,maven," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="概述前述文章谈了使用Bndtools在Eclipse中快速进行OSGi Bundle开发调试。这在产品功能开发阶段是高效的，在面对产品的后续测试、打包分发，特别是自动打包、测试及部署过程，现有的工程结构将不能满足要求。采用Maven对项目生命周期进行管理变得非常重要。当然，也可以采用Gradle进行项目管理，大家可以自己去尝试。
通过Maven进行Bundle开发###Maven Bundle P">
<meta property="og:type" content="article">
<meta property="og:title" content="Apache Karaf构建动态模块化应用(3)--使用Maven自动打包部署Bundle">
<meta property="og:url" content="http://chrisrc.me/2015/07/31/java-osgi-dynamic-app-with-karaf-maven/index.html">
<meta property="og:site_name" content="山中，山外与俯瞰">
<meta property="og:description" content="概述前述文章谈了使用Bndtools在Eclipse中快速进行OSGi Bundle开发调试。这在产品功能开发阶段是高效的，在面对产品的后续测试、打包分发，特别是自动打包、测试及部署过程，现有的工程结构将不能满足要求。采用Maven对项目生命周期进行管理变得非常重要。当然，也可以采用Gradle进行项目管理，大家可以自己去尝试。
通过Maven进行Bundle开发###Maven Bundle P">
<meta property="og:image" content="http://chrisrc.me/images/java_osgi_maven_build.png">
<meta property="og:image" content="http://chrisrc.me/images/java_osgi_maven_karaf_feature.png">
<meta property="og:updated_time" content="2016-12-17T04:03:28.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Apache Karaf构建动态模块化应用(3)--使用Maven自动打包部署Bundle">
<meta name="twitter:description" content="概述前述文章谈了使用Bndtools在Eclipse中快速进行OSGi Bundle开发调试。这在产品功能开发阶段是高效的，在面对产品的后续测试、打包分发，特别是自动打包、测试及部署过程，现有的工程结构将不能满足要求。采用Maven对项目生命周期进行管理变得非常重要。当然，也可以采用Gradle进行项目管理，大家可以自己去尝试。
通过Maven进行Bundle开发###Maven Bundle P">
<meta name="twitter:image" content="http://chrisrc.me/images/java_osgi_maven_build.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://chrisrc.me/2015/07/31/java-osgi-dynamic-app-with-karaf-maven/"/>





  <title> Apache Karaf构建动态模块化应用(3)--使用Maven自动打包部署Bundle | 山中，山外与俯瞰 </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">山中，山外与俯瞰</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">一次永不停止的探索之旅</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-dcos">
          <a href="/dcos" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-book"></i> <br />
            
            DC/OS
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="st-search-show-outputs">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <form class="site-search-form">
  <input type="text" id="st-search-input" class="st-search-input st-default-search-input" />
</form>

<script type="text/javascript">
  (function(w,d,t,u,n,s,e){w['SwiftypeObject']=n;w[n]=w[n]||function(){
    (w[n].q=w[n].q||[]).push(arguments);};s=d.createElement(t);
    e=d.getElementsByTagName(t)[0];s.async=1;s.src=u;e.parentNode.insertBefore(s,e);
  })(window,document,'script','//s.swiftypecdn.com/install/v2/st.js','_st');

  _st('install', 'R9GcmhBwxpsAGLNCsquZ','2.0.0');
</script>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
  <link itemprop="mainEntityOfPage" href="http://chrisrc.me/2015/07/31/java-osgi-dynamic-app-with-karaf-maven/">

  <span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="name" content="chrisrc">
    <meta itemprop="description" content="">
    <meta itemprop="image" content="/images/avatar.gif">
  </span>

  <span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="山中，山外与俯瞰">
    <span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
      <img style="display:none;" itemprop="url image" alt="山中，山外与俯瞰" src="">
    </span>
  </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Apache Karaf构建动态模块化应用(3)--使用Maven自动打包部署Bundle
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2015-07-31T16:18:42+08:00">
                2015-07-31
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/OSGi/" itemprop="url" rel="index">
                    <span itemprop="name">OSGi</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <a href="/2015/07/31/java-osgi-dynamic-app-with-karaf-maven/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="2015/07/31/java-osgi-dynamic-app-with-karaf-maven/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          

          
          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h2><p>前述文章谈了使用Bndtools在Eclipse中快速进行OSGi Bundle开发调试。这在产品功能开发阶段是高效的，<br>在面对产品的后续测试、打包分发，特别是自动打包、测试及部署过程，现有的工程结构将不能满足要求。采用Maven对项目<br>生命周期进行管理变得非常重要。<br>当然，也可以采用Gradle进行项目管理，大家可以自己去尝试。</p>
<h2 id="通过Maven进行Bundle开发"><a href="#通过Maven进行Bundle开发" class="headerlink" title="通过Maven进行Bundle开发"></a>通过Maven进行Bundle开发</h2><p>###Maven Bundle Plugin<br>在Bndtools之前的很长一段时间里，进行OSGi Bundle开发最通用的方式就是使用<a href="http://felix.apache.org/documentation/subprojects/apache-felix-maven-bundle-plugin-bnd.html" target="_blank" rel="external">Maven Bundle Plugin</a>。<br>Maven Bundle Plugin的底层依赖bnd工具实现bundle的分析，同时结合插件的配置，来构建bundle。采用此插件时，POM的package类型要设置为<code>bundle</code>。</p>
<p>采用Maven Bundle Plugin的另一个好处时，原生的Maven支持，因此不再局限于Eclipse，也可以在Intellij IDEA，Netbeans等IDE工具中进行开发。</p>
<p>###Bnd Maven Plugin<br>Bndtools自 2.4.1版本开始，发布了<a href="https://github.com/bndtools/bnd/tree/master/bnd-maven-plugin" target="_blank" rel="external">bnd-maven-plugin</a>，通过与工程中的bnd相结合，可以提供工程的Maven支持。</p>
<p>考虑到Bndtools在开发调试OSGi bundle时的强大功能支持，建议使用此Maven插件替代上面的Maven Bundle Plugin。此插件的另一个优势在于，POM中的<br>package类型保持为<code>jar</code>即可。</p>
<h2 id="采用Bnd-Maven-Plugin管理Bundle开发"><a href="#采用Bnd-Maven-Plugin管理Bundle开发" class="headerlink" title="采用Bnd Maven Plugin管理Bundle开发"></a>采用Bnd Maven Plugin管理Bundle开发</h2><p>基于Maven Bundle Plugin的开发在网上有很多教程，这里不再详述。</p>
<p>Bndtools和Bnd Maven Plugin的强大功能来自于Bnd工具，因此熟悉Bnd的功能是非常必要的，Bnd的详细功能请参考<a href="http://bnd.bndtools.org/" target="_blank" rel="external">Bnd官方文档</a></p>
<h3 id="准备工作"><a href="#准备工作" class="headerlink" title="准备工作"></a>准备工作</h3><p>当前，由于Maven Jar Plugin的缺陷，位于target/classes/META-INF/MANIFEST.MF的文件会被maven-jar-plugin忽略，解决方案是在<br>maven-jar-plugin的POM配置中明确指明要打包的MANIFEST.MF文件：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">plugin</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.maven.plugins<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>maven-jar-plugin<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">configuration</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">archive</span>&gt;</span></div><div class="line">      <span class="tag">&lt;<span class="name">manifestFile</span>&gt;</span>$&#123;project.build.outputDirectory&#125;/META-INF/MANIFEST.MF<span class="tag">&lt;/<span class="name">manifestFile</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">archive</span>&gt;</span></div><div class="line">  <span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">plugin</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p>maven-jar-plugin的配置可以放在工程的Parent POM中。</p>
<h3 id="项目结构"><a href="#项目结构" class="headerlink" title="项目结构"></a>项目结构</h3><p>前一篇文章使用Bndtools构建了greeter-api, greeter-service 和 greeter-client三个工程，因此，当前的工程结构是：</p>
<p>greeter-osgi-example  </p>
<pre><code>|----pom.xml      #Parent POM  
|----greeter-api  
          |----pom.xml  
          |----bnd.bnd  
          |---- ...  
|----greeter-service  
          |----pom.xml  
          |----bnd.bnd  
          |---- ...  
|----greeter-client  
          |----pom.xml  
          |----bnd.bnd  
          |---- ...  
</code></pre><p><strong>Parent POM</strong> 定义如下：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">project</span> <span class="attr">xmlns</span>=<span class="string">"http://maven.apache.org/POM/4.0.0"</span> <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></div><div class="line">         <span class="attr">xsi:schemaLocation</span>=<span class="string">"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"</span>&gt;</div><div class="line">    <span class="tag">&lt;<span class="name">modelVersion</span>&gt;</span>4.0.0<span class="tag">&lt;/<span class="name">modelVersion</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.example.greeter<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>greeter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.0.0-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">packaging</span>&gt;</span>pom<span class="tag">&lt;/<span class="name">packaging</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">modules</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">module</span>&gt;</span>greeter-api<span class="tag">&lt;/<span class="name">module</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">module</span>&gt;</span>greeter-service<span class="tag">&lt;/<span class="name">module</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">module</span>&gt;</span>greeter-client<span class="tag">&lt;/<span class="name">module</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">module</span>&gt;</span>greeter-feature-service<span class="tag">&lt;/<span class="name">module</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">module</span>&gt;</span>greeter-feature-client<span class="tag">&lt;/<span class="name">module</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">modules</span>&gt;</span></div><div class="line"></div><div class="line">    <span class="tag">&lt;<span class="name">build</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">pluginManagement</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">plugins</span>&gt;</span></div><div class="line">                <span class="tag">&lt;<span class="name">plugin</span>&gt;</span></div><div class="line">                    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.maven.plugins<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">                    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>maven-jar-plugin<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">                    <span class="tag">&lt;<span class="name">configuration</span>&gt;</span></div><div class="line">                        <span class="tag">&lt;<span class="name">useDefaultManifestFile</span>&gt;</span>true<span class="tag">&lt;/<span class="name">useDefaultManifestFile</span>&gt;</span></div><div class="line">                    <span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></div><div class="line">                <span class="tag">&lt;/<span class="name">plugin</span>&gt;</span></div><div class="line">            <span class="tag">&lt;/<span class="name">plugins</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">pluginManagement</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">build</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">project</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p><strong>greeter-api</strong> 的POM定义如下：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">project</span> <span class="attr">xmlns</span>=<span class="string">"http://maven.apache.org/POM/4.0.0"</span> <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></div><div class="line">         <span class="attr">xsi:schemaLocation</span>=<span class="string">"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"</span>&gt;</div><div class="line">    <span class="tag">&lt;<span class="name">modelVersion</span>&gt;</span>4.0.0<span class="tag">&lt;/<span class="name">modelVersion</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">parent</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.example.greeter<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>greeter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.0.0-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">parent</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.example.greeter<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>greeter-api<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">packaging</span>&gt;</span>jar<span class="tag">&lt;/<span class="name">packaging</span>&gt;</span></div><div class="line"></div><div class="line">    <span class="tag">&lt;<span class="name">build</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">plugins</span>&gt;</span></div><div class="line">            <span class="tag">&lt;<span class="name">plugin</span>&gt;</span></div><div class="line">                <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>biz.aQute.bnd<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">                <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>bnd-maven-plugin<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">                <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.4.1<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line">                <span class="tag">&lt;<span class="name">executions</span>&gt;</span></div><div class="line">                    <span class="tag">&lt;<span class="name">execution</span>&gt;</span></div><div class="line">                        <span class="tag">&lt;<span class="name">goals</span>&gt;</span></div><div class="line">                            <span class="tag">&lt;<span class="name">goal</span>&gt;</span>bnd-process<span class="tag">&lt;/<span class="name">goal</span>&gt;</span></div><div class="line">                        <span class="tag">&lt;/<span class="name">goals</span>&gt;</span></div><div class="line">                    <span class="tag">&lt;/<span class="name">execution</span>&gt;</span></div><div class="line">                <span class="tag">&lt;/<span class="name">executions</span>&gt;</span></div><div class="line">            <span class="tag">&lt;/<span class="name">plugin</span>&gt;</span></div><div class="line">        <span class="tag">&lt;/<span class="name">plugins</span>&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">build</span>&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">project</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p><strong>greeter-service</strong> 和 <strong>greeter-client</strong> 的POM定义请参考工程代码<a href="https://github.com/christtrc/greeter-osgi-example" target="_blank" rel="external">greeter-osgi-example</a>。</p>
<h3 id="编译、打包、-发布"><a href="#编译、打包、-发布" class="headerlink" title="编译、打包、 发布"></a>编译、打包、 发布</h3><p>在Eclipse中，选择单个工程或者主POM，右键，run执行时选择Maven Build，在运行配置的Goals里输入<code>clean install</code>，然后运行  </p>
<p><img src="/images/java_osgi_maven_build.png" alt="Osgi Maven Build"></p>
<p><strong>提示：如果多个团队协作，其他团队对当前团队的工程产生依赖，可以执行maven的deploy动作，将工程构件发布到内部Nexus仓库，其他团队可以通过<br>下载Maven依赖进行开发。也可以在Bndtools的Bundle仓库添加内部的Nexus仓库进行Bundle依赖开发。</strong></p>
<h3 id="小结"><a href="#小结" class="headerlink" title="小结"></a>小结</h3><p>基于Maven的Bundle开发对使用Bndtools进行开发完全没有侵入性，因此两种开发模式可以完美并存，同时，基于Maven的项目管理也为自动化测试，打包部署等<br>提供了支撑。</p>
<h2 id="通过Apache-Karaf的Feature特性进行Bundle分组管理"><a href="#通过Apache-Karaf的Feature特性进行Bundle分组管理" class="headerlink" title="通过Apache Karaf的Feature特性进行Bundle分组管理"></a>通过Apache Karaf的Feature特性进行Bundle分组管理</h2><p>关于<a href="http://karaf.apache.org/" target="_blank" rel="external">Apache Karaf</a>的详细介绍将在下一篇文章中展开，这里先讨论Bundle的分组管理。</p>
<p>基于Bundle粒度化开发时，我们将获得很多的bundle，这些bundle如何进行管理内？一种方式时所有的bundle直接混合部署，这对几个，十几个bundle的情况<br>是可以接受的，当bundle的数量达到几十，上百甚至上千个时，混合部署将变得非常复杂而且容易出错。</p>
<p>Apache Karaf的Feature提供了一种通过Feature XML描述文件对bundle进行分组管理，最终形成一棵Bundle树,例如：<br><img src="/images/java_osgi_maven_karaf_feature.png" alt="Karaf Feature"></p>
<p>Greeter OSGi Example中，我们建立了两个Feature：  </p>
<ul>
<li>greeter-service  包括：<code>greeter-api</code> 和 <code>greeter-service</code> 两个bundle</li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">feature</span> <span class="attr">name</span>=<span class="string">"greeter-service"</span> <span class="attr">version</span>=<span class="string">"$&#123;project.version&#125;"</span> <span class="attr">install</span>=<span class="string">"auto"</span>&gt;</span></div><div class="line"></div><div class="line">  <span class="tag">&lt;<span class="name">feature</span>&gt;</span>cellar<span class="tag">&lt;/<span class="name">feature</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">feature</span>&gt;</span>cellar-dosgi<span class="tag">&lt;/<span class="name">feature</span>&gt;</span></div><div class="line"></div><div class="line">  <span class="tag">&lt;<span class="name">bundle</span>&gt;</span>mvn:org.example.greeter/greeter-api/1.0.0-SNAPSHOT<span class="tag">&lt;/<span class="name">bundle</span>&gt;</span></div><div class="line">  <span class="tag">&lt;<span class="name">bundle</span>&gt;</span>mvn:org.example.greeter/greeter-service/1.0.0-SNAPSHOT<span class="tag">&lt;/<span class="name">bundle</span>&gt;</span></div><div class="line"></div><div class="line"><span class="tag">&lt;/<span class="name">feature</span>&gt;</span></div></pre></td></tr></table></figure>
<ul>
<li>greeter-client  包括：<code>greeter-api</code> 和 <code>greeter-client</code> 两个bundle</li>
</ul>
<pre><code class="xml"><span class="tag">&lt;<span class="name">feature</span> <span class="attr">name</span>=<span class="string">"greeter-client"</span> <span class="attr">version</span>=<span class="string">"${project.version}"</span> <span class="attr">install</span>=<span class="string">"auto"</span>&gt;</span>

    <span class="tag">&lt;<span class="name">feature</span>&gt;</span>cellar<span class="tag">&lt;/<span class="name">feature</span>&gt;</span>
    <span class="tag">&lt;<span class="name">feature</span>&gt;</span>cellar-dosgi<span class="tag">&lt;/<span class="name">feature</span>&gt;</span>

    <span class="tag">&lt;<span class="name">bundle</span>&gt;</span>mvn:org.example.greeter/greeter-api/1.0.0-SNAPSHOT<span class="tag">&lt;/<span class="name">bundle</span>&gt;</span>
    <span class="tag">&lt;<span class="name">bundle</span>&gt;</span>mvn:org.example.greeter/greeter-client/1.0.0-SNAPSHOT<span class="tag">&lt;/<span class="name">bundle</span>&gt;</span>

  <span class="tag">&lt;/<span class="name">feature</span>&gt;</span>
</code></pre>
<p>关于这两个Feature的应用，将在后续文中逐步介绍。</p>
<h2 id="结论"><a href="#结论" class="headerlink" title="结论"></a>结论</h2><ul>
<li>通过本文，我们可以发现，随着Bndtools功能的不断完善，基于Maven管理OSGi Bundle变得非常简单。</li>
<li>采用Maven管理OSGi Bundle的优势我们将在后续文章中逐步展示。</li>
</ul>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/osgi/" rel="tag"># osgi</a>
          
            <a href="/tags/karaf/" rel="tag"># karaf</a>
          
            <a href="/tags/bndtools/" rel="tag"># bndtools</a>
          
            <a href="/tags/maven/" rel="tag"># maven</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2015/07/29/java-osgi-dynamic-app-with-karaf-bndtools/" rel="next" title="Apache Karaf构建动态模块化应用(2)--使用Bndtools开发Bundle">
                <i class="fa fa-chevron-left"></i> Apache Karaf构建动态模块化应用(2)--使用Bndtools开发Bundle
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2015/08/02/java-osgi-dynamic-app-with-karaf/" rel="prev" title="Apache Karaf构建动态模块化应用(4)--了解Apache Karaf">
                Apache Karaf构建动态模块化应用(4)--了解Apache Karaf <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
        <div class="ds-share flat" data-thread-key="2015/07/31/java-osgi-dynamic-app-with-karaf-maven/"
     data-title="Apache Karaf构建动态模块化应用(3)--使用Maven自动打包部署Bundle"
     data-content=""
     data-url="http://chrisrc.me/2015/07/31/java-osgi-dynamic-app-with-karaf-maven/">
  <div class="ds-share-inline">
    <ul  class="ds-share-icons-16">

      <li data-toggle="ds-share-icons-more"><a class="ds-more" href="javascript:void(0);">分享到：</a></li>
      <li><a class="ds-weibo" href="javascript:void(0);" data-service="weibo">微博</a></li>
      <li><a class="ds-qzone" href="javascript:void(0);" data-service="qzone">QQ空间</a></li>
      <li><a class="ds-qqt" href="javascript:void(0);" data-service="qqt">腾讯微博</a></li>
      <li><a class="ds-wechat" href="javascript:void(0);" data-service="wechat">微信</a></li>

    </ul>
    <div class="ds-share-icons-more">
    </div>
  </div>
</div>
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="2015/07/31/java-osgi-dynamic-app-with-karaf-maven/"
           data-title="Apache Karaf构建动态模块化应用(3)--使用Maven自动打包部署Bundle" data-url="http://chrisrc.me/2015/07/31/java-osgi-dynamic-app-with-karaf-maven/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.gif"
               alt="chrisrc" />
          <p class="site-author-name" itemprop="name">chrisrc</p>
          <p class="site-description motion-element" itemprop="description"></p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">69</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">21</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">162</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/christtrc" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://twitter.com/chrisrc" target="_blank" title="Twitter">
                  
                    <i class="fa fa-fw fa-twitter"></i>
                  
                  Twitter
                </a>
              </span>
            
          
        </div>

        
        
          <div class="cc-license motion-element" itemprop="license">
            <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" target="_blank">
              <img src="/images/cc-by-nc-sa.svg" alt="Creative Commons" />
            </a>
          </div>
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#概述"><span class="nav-number">1.</span> <span class="nav-text">概述</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#通过Maven进行Bundle开发"><span class="nav-number">2.</span> <span class="nav-text">通过Maven进行Bundle开发</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#采用Bnd-Maven-Plugin管理Bundle开发"><span class="nav-number">3.</span> <span class="nav-text">采用Bnd Maven Plugin管理Bundle开发</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#准备工作"><span class="nav-number">3.1.</span> <span class="nav-text">准备工作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#项目结构"><span class="nav-number">3.2.</span> <span class="nav-text">项目结构</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#编译、打包、-发布"><span class="nav-number">3.3.</span> <span class="nav-text">编译、打包、 发布</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#小结"><span class="nav-number">3.4.</span> <span class="nav-text">小结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#通过Apache-Karaf的Feature特性进行Bundle分组管理"><span class="nav-number">4.</span> <span class="nav-text">通过Apache Karaf的Feature特性进行Bundle分组管理</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#结论"><span class="nav-number">5.</span> <span class="nav-text">结论</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2015 - 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">
     
      <a href="mailto:chrisrc@qq.com">chrisrc</a> 
    
  </span>
  <span class="author"> &nbsp; QQ: 58122705 </span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"chrisrc"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    
    <script src="/lib/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/js/src/hook-duoshuo.js"></script>
  








  
  

  

  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  
<script>
(function(){
   var src = (document.location.protocol == "http:") ? "http://js.passport.qihucdn.com/11.0.1.js?ed903ab05fbc5fa64d95e4bfbf3d3a04":"https://jspassport.ssl.qhimg.com/11.0.1.js?ed903ab05fbc5fa64d95e4bfbf3d3a04";
   document.write('<script src="' + src + '" id="sozz"><\/script>');
})();
</script>

  


</body>
</html>
